/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package limousine.dbservice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.DataSource;
import limousine.util.Func;
import limousine.vo.TaxiTariff;

/**
 *
 * @author Tailer
 */
public class TaxiTariffService {

    private DataSource ds = null;
    private Func func = new Func();

    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
    }

    public TaxiTariff getByTaxiRateId(String taxiRateId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * From TAXI_TARIFF where TAXI_RATE_ID = '" + taxiRateId.trim() + "' ");
        TaxiTariff o = null;
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            if (rs.next()) {
                o = new TaxiTariff();
                o.setTaxiRateId(rs.getString("TAXI_RATE_ID"));
                o.setDistination(rs.getString("DISTINATION"));
                o.setTaxiSize(rs.getInt("TAXI_SIZE"));
                o.setOneWay(rs.getInt("ONE_WAY"));
                o.setRoundTrip(rs.getInt("ROUND_TRIP"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(rs.getDate("CREATE_DATE"));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(rs.getDate("UPDATE_DATE"));
                o.setTaxiTariffOrder(rs.getInt("TAXI_TARIFF_ORDER"));
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return o;
    }

    public boolean insert(TaxiTariff o) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO TAXI_TARIFF(TAXI_RATE_ID,DISTINATION,TAXI_SIZE,ONE_WAY,ROUND_TRIP,CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE,TAXI_TARIFF_ORDER ) ");
        sql.append("VALUES (?,?,?,?,?,?,?,?,?,?)");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getTaxiRateId());
            stmt.setString(2, o.getDistination());
            stmt.setInt(3, o.getTaxiSize());
            stmt.setInt(4, o.getOneWay());
            stmt.setInt(5, o.getRoundTrip());
            stmt.setString(6, o.getCreateBy());
            stmt.setTimestamp(7, func.getTimestamp());
            stmt.setString(8, o.getUpdateBy());
            stmt.setTimestamp(9, func.getTimestamp());
            stmt.setInt(10, o.getTaxiTariffOrder());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(TaxiTariff o) {
        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE TAXI_TARIFF ");
        sql.append("SET DISTINATION=?,TAXI_SIZE=?,ONE_WAY=?,ROUND_TRIP=?,UPDATE_BY=?,UPDATE_DATE=?,TAXI_TARIFF_ORDER=?  ");
        sql.append("where TAXI_RATE_ID='" + o.getTaxiRateId() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.setString(1, o.getDistination());
            stmt.setInt(2, o.getTaxiSize());
            stmt.setInt(3, o.getOneWay());
            stmt.setInt(4, o.getRoundTrip());
            stmt.setString(5, o.getUpdateBy());
            stmt.setTimestamp(6, func.getTimestamp());
            stmt.setInt(7, o.getTaxiTariffOrder());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteByTaxiRateId(String taxiRateId) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from TAXI_TARIFF where TAXI_RATE_ID = '" + taxiRateId.trim() + "' ");
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql.toString());
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteBySQL(String sql) {
        try {
            Connection conn = ds.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
            stmt.close();
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}
